--障碍物工厂

local Block = require "app.models.Block"
local BlockFactory = class("BlockFactory")

function BlockFactory:ctor(node)
	self.BlockArray = {}
	self.node = node
	self.idAcc = 0
end

function BlockFactory:Add(x, y, index)
	local b = Block.new(self.node)
	--设置障碍物
	b:Set(index)
	b:SetPos(x, y)
	b:SetBlock()
	--加入到数组
	table.insert( self.BlockArray, b )	
end

function BlockFactory:Remove( other )
	local b, index = self:Hit(other)

	if nil ~= b then
		b:Clear()
		table.remove(self.BlockArray, index)
	end
end

function BlockFactory:Hit( other )
	for index, b in ipairs(self.BlockArray) do
		if b:Hit(other) then
			return b, index
		end
	end

	return nil, -1
end

function BlockFactory:Reset( )
	for _, b in ipairs(self.BlockArray) do
		b:Clear()
	end
end

function BlockFactory:Save(file)
	for _, b in ipairs(self.BlockArray) do
		file:write(string.format("{ x = %d, y = %d, index = %d },\n", b.x, b.y, b.index))
	end
end


return BlockFactory